package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 1.新增用户(实现注册用户功能)
 * 2.根据用户名查询用户信息
 * 3.根据用户ID查询用户信息
 */
public class UserDao {
    public void addUser(User user){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="insert into user values(null,?,?,?,null)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getNickName());
            preparedStatement.setString(3, user.getPassword());
            int ret=preparedStatement.executeUpdate();
            if (ret==1){
                System.out.println("注册成功");
            }else{
                System.out.println("注册失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public User queryInfoByUserName(String username){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from user where userName= ? ";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            resultSet=preparedStatement.executeQuery();
            if (resultSet.next()){
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setNickName(resultSet.getString("nickname"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return  null;
    }
    public User queryInfoByUserId(int userId){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from user where userId= ? ";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            resultSet=preparedStatement.executeQuery();
            if (resultSet.next()){
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setNickName(resultSet.getString("nickname"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return  null;
    }
    public void update(int userId,String password,String nickname,String picture){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="update user set nickname =?,password=?,picture=? where userId=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,nickname);
            preparedStatement.setString(2,password);
            preparedStatement.setString(3,picture);
            preparedStatement.setInt(4,userId);
            int ret=preparedStatement.executeUpdate();
            if (ret==1){
                System.out.println("更新成功");
            }else{
                System.out.println("更新失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,null);
        }
    }
}
